package com.cy.store.service.impl;

import com.cy.store.entity.Cart;
import com.cy.store.entity.Product;
import com.cy.store.mapper.CartMapper;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.ICartService;
import com.cy.store.service.ex.*;
import com.cy.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 更改商品的购物数量
     *
     * @param cid
     * @param uid
     * @param username
     * @return
     */
    @Override
    public Integer addNum(Integer cid, String ope, Integer uid, String username) {
        Cart cart = cartMapper.findByCid(cid);
        if (cart == null) {
            throw new CartNotFoundException("商品数据不存在");
        }
        if (!cart.getUid().equals(uid)) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num;
        if (ope.equals("+")) {
            num = cart.getNum() + 1;
        } else if (ope.equals("-")) {
            num = cart.getNum() - 1;
        } else {
            throw new OpeException("运算符数据异常");
        }
        //如果不为0 执行更新操作
        if (num > 0) {
            cartMapper.updateNumByCid(cid, num, username, new Date());
            return num;
        } else {//如果运算后为0执行删除操作
            //mapper设计接口传入参数为 List<Integer> 所以将cid 包装为 List<Integer>中
            Integer[] cids = new Integer[]{cid};
            cartMapper.deleteByCid(cids);
            return 0;
        }

    }

    /**
     * 向提交订单页面显示数据
     *
     * @param uid
     * @param cids
     * @return
     */
    @Override
    public List<CartVo> getVOByCid(Integer uid, Integer[] cids) {
        for (Integer cid : cids) {//判断购物车数据是否为本用户
            if (!cartMapper.findByCid(cid).getUid().equals(uid)) {
                throw new AccessDeniedException("数据非法访问");
            }
        }
        List<CartVo> list = cartMapper.findVOByCid(cids);
        return list;
    }

    /**
     * 删除购物车数据
     *
     * @param uid
     * @param cids
     */
    @Override
    public void deleteCart(Integer uid, Integer[] cids) {
        //先遍历每一个cid 判断是否有非法访问的数据 或数据不存在
        for (Integer cid : cids) {
            Cart cart = cartMapper.findByCid(cid);
            if (cart == null) {
                throw new CartNotFoundException("购物车数据异常");
            } else if (!cart.getUid().equals(uid)) {
                throw new AccessDeniedException("数据非法访问");
            }
        }
        //如果数据不存在异常 则执行删除操作
        cartMapper.deleteByCid(cids);
    }

    /**
     * 显示购物车数据
     *
     * @param uid
     * @return
     */
    @Override
    public List<CartVo> getVOByUid(Integer uid) {
        List<CartVo> list = cartMapper.findVOByUid(uid);
        return list;
    }

    /**
     * 添加商品到购物车
     *
     * @param uid
     * @param pid
     * @param amount
     * @param username
     */
    @Override
    public void addToCart(Integer uid, Integer pid, Integer amount, String username) {
        //查询购物车是否已存在该商品
        Cart cart = cartMapper.findByUidAndPid(uid, pid);
        if (cart == null) {//表示购物车还不存在该商品
            Product product = productMapper.findById(pid);
            //将上面创建的cart（此时为null）封装属性
            cart = new Cart();
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setPrice(product.getPrice());
            cart.setNum(amount);

            //补全四项日志
            cart.setCreatedTime(new Date());
            cart.setCreatedUser(username);
            cart.setModifiedTime(new Date());
            cart.setModifiedUser(username);

            //执行数据的插入
            Integer rows = cartMapper.insert(cart);
            if (rows != 1) {
                throw new InsertException("添加购物车数据异常");
            }
            return;
        } else {//表示购物车已存在该商品 需要更新数据库的num值
            Integer cid = cart.getCid();
            Integer num = amount + cart.getNum();
            Integer rows = cartMapper.updateNumByCid(cid, num, username, new Date());
            if (rows != 1) {
                throw new UpdateException("更新数据库信息异常");
            }
        }
    }
}
